package app.cloudstringers;

import utils.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import controller.GetTaskManagerAsync;
import define.Constants;
import define.DefaultValue;
import define.SharedPreference;

public class TaskManager extends Activity {
	/**
	 * String section
	 */
	private static final String TAG = TaskManager.class.getSimpleName();

	/**
	 * Listener section
	 */
	private OnItemClickListener mListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Log.i(TAG, "HERE");
			/**
			 * Show info dialog
			 */
			Dialog mDialog = new Dialog(TaskManager.this);
			mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			mDialog.setContentView(R.layout.dialog_task_manager_info);

			initalVariables(mDialog);
			initialData(position);

			mDialog.show();
		}
	};
	
	/**
	 * Listener section
	 */
	private OnClickListener mListenerCancel = new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if (mDialog.isShowing()) {
				mDialog.dismiss();
			}
		}
	};
	
	/**
	 * View section
	 */
	private AlertDialog.Builder mAdLogout;
	private AlertDialog.Builder mAdExit;
	private Dialog mDialog;
	private ImageButton mIbtnCancel;
	public static ListView mLv;
	private TextView mTvDescription;
	private TextView mTvDuration;
	private TextView mTvState;
	private TextView mTvTimeZone;

	
	/**
	 * Others section
	 */
	private SharedPreferences.Editor mSpEditor;
	private SharedPreferences mSpSettings;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_task_manager);

		Constants.utils = new Utils(this);

		// Initial dialogs & variables
		initialDialogs();
		initialVariables();
		
		mLv.setOnItemClickListener(mListener);
	}

	/**
	 * On clicked method
	 */
	public void onClickTaskManager(View v) {
		switch (v.getId()) {
		case R.id.ibtn_home_in_task_manager:
			finish();
			startActivity(new Intent(TaskManager.this, Home.class));
			break;
		case R.id.ibtn_logout_in_task_manager:
			mAdLogout.show();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			mAdExit.show();
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onResume() {
		super.onResume();

		if (Constants.utils.isNetworkOnline()) {
			// Get data from shared preference
			mSpSettings = getSharedPreferences(SharedPreference.PREFS,
					Context.MODE_PRIVATE);
			mSpEditor = mSpSettings.edit();

			// This always happened when was disconnected
			// If the connection dialog has shown, dismiss it before show new
			// dialog
			Utils.hideConnectionDialog();
			
			new GetTaskManagerAsync(this).execute(Home.ID);
		} else {
			Utils.hideConnectionDialog();
			Utils.showConnectionDialog();
		}
	}

	/**
	 * Initial methods
	 */
	/**
	 * Initialize methods
	 */

	private void initialData(int position) {
//		mIbtnCancel.setOnClickListener(mListenerCancel);

//		mTvDescription.setText(mLlTaskManger.get(position).getDescription());
//		mTvDuration.setText(mLlTaskManger.get(position).getDuration());
//		mTvState.setText(mLlTaskManger.get(position).getState());
//		mTvTimeZone.setText(mLlTaskManger.get(position).getTimeZone());
	}

	private void initialDialogs() {
		mAdLogout = new AlertDialog.Builder(this)
		.setTitle(getString(R.string.title_logout))
		.setCancelable(false)
		.setMessage(getString(R.string.content_logout))
		.setPositiveButton(R.string.yes, new OnClickListener() {
			@Override
			public void onClick(DialogInterface d, int whichButton) {
				// set data for shared preferences
				mSpEditor.putString(
						SharedPreference.PREFS_KEY_USER_NAME, "user");
				mSpEditor.putString(
						SharedPreference.PREFS_KEY_GLOBAL_ID,
						DefaultValue.DEFAULT_GLOBAL_ID + "");
				mSpEditor.commit();

				// new intent to login page
				finish();
				startActivity(new Intent(TaskManager.this, Login.class));
			}
		})
		.setNegativeButton(R.string.no, new OnClickListener() {

			@Override
			public void onClick(DialogInterface d, int which) {
			}
		});

		mAdExit = new AlertDialog.Builder(TaskManager.this)
		.setTitle(R.string.title_exit)
		.setIcon(R.drawable.ic_info_normal)
		.setCancelable(false)
		.setMessage(R.string.content_exit)
		.setPositiveButton(R.string.ok, new OnClickListener() {
			@Override
			public void onClick(DialogInterface d, int whichButton) {
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		})
		.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface d, int whichButton) {
			}
		});
	}

	private void initialVariables() {
		mLv = (ListView) findViewById(R.id.lv_in_task_manager);
	}

	private void initalVariables(Dialog d) {
		mIbtnCancel = (ImageButton) d
				.findViewById(R.id.ibtn_cancel_in_task_manager_info);
		mTvDescription = (TextView) d
				.findViewById(R.id.tv_description_in_task_manager_info);
		mTvDuration = (TextView) d
				.findViewById(R.id.tv_duration_in_task_manager_info);
		mTvState = (TextView) d
				.findViewById(R.id.tv_state_in_task_manager_info);
		mTvTimeZone = (TextView) d
				.findViewById(R.id.tv_time_zone_in_task_manager_info);
	}

	/**
	 * Basic methods
	 */
}
